###########################################
#                                         #
#                MACROS                   #
#                                         #
###########################################

# Echoing in different colors (thanks to CMAKE)
CMAKE_COMMAND = /usr/bin/cmake
PRINT = @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR)
NEWLINE = @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --red ""

# Basic remove
REMOVE = \rm -f -r *~ *.o

# Define topdir
TOPDIR = ..

# Define testdir
TESTDIR = 

# Include directory
INC = ../include
PS = ../playerstage

# Flags
CC = gcc
NLFLAGS = -Wall -c -g
CFLAGS = -Wall -g

PP = g++
PLAYER = `pkg-config --cflags playerc++` `pkg-config --libs playerc++`

# All executable files
ALL = localization

# All object files which should be erased
OBJ = localization.o kalman.o listas.o Gmatrix.o

###########################################
#                                         #
#                TARGETS                  #
#                                         #
###########################################

# Compile and Run everything
all: $(ALL)

# Delete every executable, .o and ~ files
clean:
	$(REMOVE) logfile $(OBJ) $(ALL)
	cd ..; $(REMOVE)
	cd $(INC); $(REMOVE)
	cd $(PS); $(REMOVE)

# Run test
run:
	$(PRINT) --red "Starting Landmark Based EKF Localization with Known Correspondences"
	$(NEWLINE)
	$(PRINT) --green "Opening Player/Stage with config file \"landmarks.cfg\"..."
	player $(PS)/landmarks.cfg &
	@sleep 4
	$(PRINT) --blue "Opening executable file \"localization\"..."
	./localization
	
# Run test saving 
log:
	$(PRINT) --red "Starting Landmark Based EKF Localization with Known Correspondences"
	$(NEWLINE)
	$(PRINT) --green "Opening Player/Stage with config file \"landmarks.cfg\"..."
	player $(PS)/landmarks.cfg &
	@sleep 4
	$(PRINT) --blue "Opening executable file \"localization\"..."
	$(PRINT) --magenta "Saving output in logfile"
	./localization > logfile

# Run test debugging
debug:
	$(PRINT) --red "Starting Landmark Based EKF Localization with Known Correspondences"
	$(NEWLINE)
	$(PRINT) --green "Opening Player/Stage with config file \"landmarks.cfg\"..."
	player $(PS)/landmarks.cfg &
	@sleep 4
	$(PRINT) --blue "Opening executable file \"localization\"..."
	gdb localization
	
# Remake
remake:
	make clean
	make
	
###########################################
#                                         #
#            DEPENDENCY FILES             #
#                                         #
###########################################
	
# If localization.cc has been changed, it will be compiled
localization: $(OBJ) $(INC)/joystick.h $(INC)/localization.h $(INC)/kalman.h $(INC)/listas.h $(INC)/Gmatrix.h
	$(PP) $(OBJ) $(PLAYER) -o localization

localization.o: $(INC)/joystick.h $(INC)/localization.h localization.cc
	$(PP) $(NLFLAGS) $(PLAYER) -I$(INC) localization.cc 

kalman.o: kalman.cpp $(INC)/kalman.h $(INC)/listas.h $(INC)/Gmatrix.h
	$(PP) $(NLFLAGS) -I$(INC) kalman.cpp

Gmatrix.o: Gmatrix.c $(INC)/Gmatrix.h
	$(PP) $(NLFLAGS) -I$(INC) Gmatrix.c

listas.o: listas.cpp $(INC)/listas.h
	$(PP) $(NLFLAGS) -I$(INC) listas.cpp
